Imports System.Data
Imports System.Data.SqlClient
Imports CAPA_Entidad
Imports CAPA_Utilitarios
Public Class DocumentoClienteDetallePickeadorUbicacionBandejaDatos

    Function registrarDocumentoAlmacenDetallePickeadorUbicacionBandeja(ByVal dadpub As DocumentoAlmacenDetallePickeadorUbicacionBandeja) As Boolean
        Dim valor As Boolean = False
        Dim conex As New Conexion
        Dim con As SqlConnection = conex.conectar()

        Try
            con.Open()

            Dim query As String = "INSERT INTO Documento_Almacen_Detalle_Pickeador_Ubicacion_Bandeja(CIA,SUC_Codigo,TDOC_Codigo,DCA_Codigo,DADPU_Codigo, " & _
                                  "ALM_Codigo,BAN_Codigo,DADPUB_Cantidad,DADPUB_FechaInicio,DADPUB_FechaFin,DADPUB_Balanza,DADPUB_Activo) " & _
                                  "VALUES(@CIA,@SUC,@TDO,@DCA,@DADPU,@ALM,@BAN,@CAN,'" & dadpub.DADPUB_FechaInicio & "',NULL,NULL,'1')"

            Dim comando As New SqlCommand(query, con)
            With comando
                .CommandText = query
                .Parameters.AddWithValue("@CIA", dadpub.CIA)
                .Parameters.AddWithValue("@SUC", dadpub.SUC_Codigo)
                .Parameters.AddWithValue("@TDO", dadpub.TDOC_Codigo)
                .Parameters.AddWithValue("@DCA", dadpub.DCA_Codigo)
                .Parameters.AddWithValue("@DADPU", dadpub.DADPU_Codigo)
                .Parameters.AddWithValue("@ALM", dadpub.ALM_Codigo)
                .Parameters.AddWithValue("@BAN", dadpub.BAN_Codigo)
                .Parameters.AddWithValue("@CAN", dadpub.DADPUB_Cantidad)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Sub actualizarFechaFinBandejaDADPUB(ByVal codPick As String, ByVal codBan As String, ByVal codPedido As String)
        Dim valor As Boolean = False
        Dim conex As New Conexion
        Dim con As SqlConnection = conex.conectar

        Try
            con.Open()

            Dim query As String = "UPDATE dadpub " & _
                                  "SET dadpub.DADPUB_FechaFin = getDate() " & _
                                  "FROM Documento_Almacen_Detalle_Pickeador_Ubicacion_Bandeja dadpub " & _
                                  "INNER JOIN Documento_Almacen_Detalle_Pickeador_Ubicacion dadpu " & _
                                  "ON dadpu.DADPU_Codigo = dadpub.DADPU_Codigo AND dadpu.DADPU_Activo = '1' " & _
                                  "INNER JOIN Documento_Almacen_Detalle_Pickeador dadp " & _
                                  "ON dadp.DADP_Codigo = dadpu.DADP_Codigo AND dadp.DCA_Codigo = dadpu.DCA_Codigo " & _
                                  "AND dadp.DADP_Activo = '1' " & _
                                  "WHERE dadp.PICK_Codigo = @codPick " & _
                                  "AND dadpub.CIA+dadpub.SUC_Codigo+dadpub.ALM_Codigo+dadpub.BAN_Codigo = @codBan " & _
                                  "AND dadpu.DCA_Codigo = @codPedido AND dadpub.DADPUB_Activo = '1' " & _
                                  "AND dadpub.DADPUB_FechaFin IS NULL"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@codPick", codPick)
                .Parameters.AddWithValue("@codBan", codBan)
                .Parameters.AddWithValue("@codPedido", codPedido)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try
    End Sub

    Sub actualizarFechaFinBandejaDCDPUBAutomatico(ByVal codPick As String, ByVal codBan As String)
        Dim valor As Boolean = False
        Dim conex As New Conexion
        Dim con As SqlConnection = conex.conectar

        Try
            con.Open()

            Dim query As String = "UPDATE dadpub " & _
                                  "SET dadpub.DADPUB_FechaFin = getDate() " & _
                                  "FROM Documento_Almacen_Detalle_Pickeador_Ubicacion_Bandeja dadpub " & _
                                  "INNER JOIN Documento_Almacen_Detalle_Pickeador_Ubicacion dadpu " & _
                                  "ON dadpu.DADPU_Codigo = dadpub.DADPU_Codigo AND dadpu.DADPU_Activo = '1' " & _
                                  "INNER JOIN Documento_Almacen_Detalle_Pickeador dadp " & _
                                  "ON dadp.DADP_Codigo = dadpu.DADP_Codigo AND dadp.DCA_Codigo = dadpu.DCA_Codigo " & _
                                  "AND dadp.DADP_Activo = '1' " & _
                                  "WHERE dadp.PICK_Codigo = @codPick " & _
                                  "AND dadpub.CIA+dadpub.SUC_Codigo+dadpub.ALM_Codigo+dadpub.BAN_Codigo = @codBan " & _
                                  "AND dadpub.DADPUB_FechaFin IS NULL AND dadpub.DADPUB_Activo = '1'"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@codPick", codPick)
                .Parameters.AddWithValue("@codBan", codBan)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try
    End Sub

    Sub actualizarCantidadBandejaDADPUB2(ByVal dadpu As Integer, ByVal bandeja As String, ByVal cant As Integer)
        Dim valor As Boolean = False
        Dim conex As New Conexion
        Dim con As SqlConnection = conex.conectar

        Try
            con.Open()

            Dim query As String = "UPDATE Documento_Almacen_Detalle_Pickeador_Ubicacion_Bandeja " & _
                                  "SET DADPUB_Cantidad = @cantidad " & _
                                  "WHERE DADPU_Codigo = @dcdpuCodigo " & _
                                  "AND CIA+SUC_Codigo+ALM_Codigo+BAN_Codigo = @bandeja " & _
                                  "AND DADPUB_Activo = '1' AND DADPUB_FechaFin IS NULL"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@cantidad", cant)
                .Parameters.AddWithValue("@bandeja", bandeja)
                .Parameters.AddWithValue("@dcdpuCodigo", dadpu)
                .ExecuteNonQuery()
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try
    End Sub

    Function verificarExistenciaBandejaDADPUB(ByVal dadpuCodigo As Integer, ByVal codigoBandeja As String) As Boolean
        Dim valor As Boolean = False
        Dim conex As New Conexion
        Dim con As SqlConnection = conex.conectar

        Try
            con.Open()

            Dim query As String = "SELECT DADPUB_Codigo " & _
                                  "FROM Documento_Almacen_Detalle_Pickeador_Ubicacion_Bandeja " & _
                                  "WHERE DADPU_Codigo = " & dadpuCodigo & _
                                  " AND CIA+SUC_Codigo+ALM_Codigo+BAN_Codigo = '" & codigoBandeja & "' " & _
                                  "AND DADPUB_Activo = '1'"

            Dim adapter As SqlDataAdapter = New SqlDataAdapter(query, con)
            Dim ds As New DataSet
            adapter.Fill(ds)

            If ds.Tables(0).Rows.Count <> 0 Then
                valor = True
            End If
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

End Class
